package com.example.demo.controller;

import com.example.demo.annotation.MyRequestBody;
import com.example.demo.config.FeatureConfig;
import com.example.demo.entity.Goods;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserDaoImpl;
import com.example.demo.service.RedisService;
import com.example.demo.service.UserService;
import com.example.demo.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author chenhuiyu
 * @Date 2023/11/8
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private RedisService redisService;

    @Autowired
    private FeatureConfig featureConfig;

    @PostMapping("/hello")
    public Result hello(@MyRequestBody String s) {
        System.err.println(s);
        log.debug("其他信息{}", featureConfig.toString());
        log.debug(s.toString());
        Result result = new Result();
        result.setMessage("success");
        result.setObject(s);
        return result;
    }

    @PostMapping("/register")
    private Result register(@MyRequestBody User user){
        Result result = new Result();
        if (user == null){
            result.setMessage("用户信息不能为空");
            return result;
        }
        if (!StringUtils.hasText(user.getUsername())){
            result.setMessage("用户名不能为空");
            return result;
        }
        if (!StringUtils.hasText(user.getPassword())){
            result.setMessage("密码不能为空");
            return result;
        }
        User _user = userService.findUserByName(user);
        if (_user != null) {
            result.setMessage("用户" + user.getUsername() + "已存在");
            return result;
        }
        User u = userService.add(user);
        result.setMessage("success");
        result.setObject(u);
        return result;
    }

    @PostMapping("/login")
    public Result login(@MyRequestBody User user) {
        log.debug("其他信息{}", featureConfig.toString());
        Result result = new Result();
        if (!StringUtils.hasText(user.getUsername()) || !StringUtils.hasText(user.getPassword())) {
            result.setMessage("请输入账号或密码");
            return result;
        }
        User u = userService.login(user);
        //
        if (u != null) {
            result.setMessage("登录成功");
            result.setObject(u);
            Map<Object, Object> _map = redisService.findByKey(u.getId() + "-" + u.getUsername());
            if (_map == null || _map.isEmpty()) {
                Map<Object, Object> map = new HashMap<>();
                map.put("id", u.getId());
                map.put("age", u.getAge());
                map.put("username", u.getUsername());
                map.put("password", u.getPassword());
                map.put("flag", u.getFlag());
                map.put("time", String.valueOf(System.currentTimeMillis()));
                redisService.add(u.getId() + "-" + u.getUsername(), map);
            } else {
                redisService.updateTimeByKey(u.getId() + "-" + u.getUsername(), 120);
            }
        } else {
            result.setMessage("登录失败");
        }
        return result;
    }

    @PostMapping("/find/goods")
    public Result findGoods(@MyRequestBody User user) {
        Result result = new Result();
        user = userService.login(user);
        Map<Object, Object> value = redisService.findByKey(user.getId() + "goods");
        boolean b = redisService.updateTimeByKey(user.getId() + "-" + user.getUsername(), 60);
        System.err.println(b);
        if (value == null) {
            List<Goods> goods = userService.findGoods(user);
            if (!goods.isEmpty()) {
                Map<Object, Object> map = new HashMap<>();
                map = goods.stream().collect(Collectors.toMap(Goods::getName, good -> good));
                redisService.add(user.getId() + "goods", map);
                result.setObject(goods);
                result.setMessage("查询成功");
            } else {
                result.setMessage("查询失败");
            }
        } else {
            result.setMessage("查询成功");
            result.setObject(value);
        }
        return result;
    }

    @PostMapping("/find/users")
    public Result findUsers(@RequestBody User user)  {
        Result result = new Result();
        dealEmptyString(user);
//        UserDaoImpl userDaoImpl = new UserDaoImpl();
        List<User> users = userService.findUsers(user);
//        List<User> users = userDaoImpl.findUsers(user);
        result.setObject(users);
        result.setMessage("success");
        return result;
    }
    @PostMapping("/toGoods")
    public Result userToGoods(@RequestBody User user) {
        Result result = userService.userToGoods(user);
        return result;
    }

    private void dealEmptyString(User user){
        if (user.getUsername().isEmpty()){
            user.setUsername(null);
        }
        if (user.getPassword().isEmpty()){
            user.setPassword(null);
        }
    }
}
